package business_opportunity

import (
	"context"
	"errors"
	businessOpportunityPb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/business_opportunity/subscription"
	"git.myscrm.cn/golang/stark/v4"
)

type subscriptionServer struct {
	subscriptionService subscription.SubscriptionServiceIface
}

func NewSubscriptionServer(subscriptionService subscription.SubscriptionServiceIface) businessOpportunityPb.SubscriptionServiceServer {
	return &subscriptionServer{
		subscriptionService: subscriptionService,
	}
}

// GetRadarSubscriptionConfig 获取线索雷达订阅设置
func (s *subscriptionServer) GetRadarSubscriptionConfig(ctx context.Context, request *businessOpportunityPb.GetRadarSubscriptionConfigRequest) (*businessOpportunityPb.GetRadarSubscriptionConfigResponse, error) {
	res, err := s.subscriptionService.GetRadarSubscriptionConfig(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetRadarSubscriptionConfig 获取线索订阅配置失败，参数:%+v，错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// SaveRadarSubscriptionConfig 保存线索雷达订阅设置
func (s *subscriptionServer) SaveRadarSubscriptionConfig(ctx context.Context, request *businessOpportunityPb.SaveRadarSubscriptionConfigRequest) (*businessOpportunityPb.SaveRadarSubscriptionConfigResponse, error) {
	res, err := s.subscriptionService.SaveRadarSubscriptionConfig(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveRadarSubscriptionConfig 保存线索订阅配置失败，参数:%+v，错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetSubscriptionPushConfig 获取订阅推送设置
func (s *subscriptionServer) GetSubscriptionPushConfig(ctx context.Context, request *businessOpportunityPb.GetSubscriptionPushConfigRequest) (*businessOpportunityPb.GetSubscriptionPushConfigResponse, error) {
	res, err := s.subscriptionService.GetSubscriptionPushConfig(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetSubscriptionPushConfig 获取推送配置失败，参数:%+v，错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// SaveSubscriptionPushConfig 保存订阅推送设置
func (s *subscriptionServer) SaveSubscriptionPushConfig(ctx context.Context, request *businessOpportunityPb.SaveSubscriptionPushConfigRequest) (*businessOpportunityPb.SaveSubscriptionPushConfigResponse, error) {
	res, err := s.subscriptionService.SaveSubscriptionPushConfig(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveSubscriptionPushConfig 保存推送配置失败，参数:%+v，错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetWxQrCode 获取公众号二维码
func (s *subscriptionServer) GetWxQrCode(ctx context.Context, request *businessOpportunityPb.GetWxQrCodeRequest) (*businessOpportunityPb.GetWxQrCodeResponse, error) {
	res, err := s.subscriptionService.GetWxQrCode(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetWxQrCode 获取公众号二维码失败，参数:%+v，错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetBindStatus 根据场景码获取公众号关注状态
func (s *subscriptionServer) GetBindStatus(ctx context.Context, request *businessOpportunityPb.GetBindStatusRequest) (*businessOpportunityPb.GetBindStatusResponse, error) {
	var errMsg string
	if request.GetSceneId() == "" {
		errMsg = "scene_id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.subscriptionService.GetBindStatus(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBindStatus 获取绑定状态，参数:%+v，错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// BindOpenIdByCard 通过小程序卡片绑定“云采购平台公众号”openid
func (s *subscriptionServer) BindOpenIdByCard(ctx context.Context, request *businessOpportunityPb.BindOpenIdByCardRequest) (*businessOpportunityPb.BindOpenIdByCardResponse, error) {
	res, err := s.subscriptionService.BindOpenIdByCard(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "BindOpenIdByCard 绑定ycg_openid，参数:%+v，错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetRadarList 获取线索雷达列表数据
func (s *subscriptionServer) GetRadarList(ctx context.Context, request *businessOpportunityPb.GetRadarListRequest) (*businessOpportunityPb.GetRadarListResponse, error) {
	resp, err := s.subscriptionService.GetRadarList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetRadarList request:%#v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *subscriptionServer) GetRadarPreCount(ctx context.Context, request *businessOpportunityPb.GetRadarPreCountRequest) (*businessOpportunityPb.GetRadarPreCountResponse, error) {
	resp, err := s.subscriptionService.GetRadarPreCount(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetRadarList request:%#v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}

func (s *subscriptionServer) RadarManualPushMessage(ctx context.Context, request *businessOpportunityPb.RadarManualPushMessageRequest) (*businessOpportunityPb.RadarManualPushMessageResponse, error) {
	resp := &businessOpportunityPb.RadarManualPushMessageResponse{}
	return resp, nil
}
